﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.skill;
using System.Threading;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;

namespace l1cs.server.model.poison
{
    public class L1ParalysisPoison : L1Poison
    {
        // 麻痺毒の性能一覧 猶予 持続 (参考値、未適用)
        // グール 20 45
        // アステ 10 60
        // 蟻穴ムカデ 14 30
        // D-グール 39 45

        private L1Character _target;
        private Timer _timer;
        private int _delay;
        private int _time;
        private int _effectId = 1;

        //private class ParalysisPoisonTimer extends Thread {
        public void ParalysisPoisonTimerSub( object state )
        {
            _target.setSkillEffect( L1SkillId.STATUS_POISON_PARALYZING, 0 );

            //try {
            Thread.Sleep( _delay ); // 麻痺するまでの猶予時間を待つ。
            //} catch (InterruptedException e) {
            //    _target
            //            .killSkillEffectTimer(STATUS_POISON_PARALYZING);
            //    return;
            //}

            // エフェクトを緑から灰色へ
            _effectId = 2;
            _target.setPoisonEffect( 2 );

            if ( _target is L1PcInstance )
            {
                L1PcInstance player = (L1PcInstance)_target;
                if ( player.isDead() == false )
                {
                    player.sendPackets( new S_Paralysis( 1, true ) ); // 麻痺状態にする
                    //_timer = new ParalysisTimer();
                    //GeneralThreadPool.getInstance().execute(_timer); // 麻痺タイマー開始
                    _timer = new Timer( new TimerCallback( ParalysisTimerSub ), null, 0, Timeout.Infinite );
                    //if ( isInterrupted() )
                    //{
                    _timer.Dispose();
                    //}
                }
            }
        }
        //}

        //private class ParalysisTimer extends Thread {
        //    override
        public void ParalysisTimerSub( object state )
        {
            _target.killSkillEffectTimer( L1SkillId.STATUS_POISON_PARALYZING );
            _target.setSkillEffect( L1SkillId.STATUS_POISON_PARALYZED, 0 );
            //try {
            Thread.Sleep( _time );
            //} catch (InterruptedException e) {
            //}

            _target.killSkillEffectTimer( L1SkillId.STATUS_POISON_PARALYZED );
            if ( _target is L1PcInstance )
            {
                L1PcInstance player = (L1PcInstance)_target;
                if ( !player.isDead() )
                {
                    player.sendPackets( new S_Paralysis( 1, false ) ); // 麻痺状態を解除する
                    cure(); // 解毒処理
                }
            }
        }
        //}

        private L1ParalysisPoison( L1Character cha, int delay, int time )
        {
            _target = cha;
            _delay = delay;
            _time = time;

            doInfection();
        }

        public static bool doInfection( L1Character cha, int delay, int time )
        {
            if ( !L1Poison.isValidTarget( cha ) )
            {
                return false;
            }

            cha.setPoison( new L1ParalysisPoison( cha, delay, time ) );
            return true;
        }

        private void doInfection()
        {
            sendMessageIfPlayer( _target, 212 );
            _target.setPoisonEffect( 1 );

            if ( _target is L1PcInstance )
            {
                //_timer = new ParalysisPoisonTimer();
                //GeneralThreadPool.getInstance().execute(_timer);
                _timer = new Timer( new TimerCallback( ParalysisPoisonTimerSub ), null, 0, Timeout.Infinite );
            }
        }

        override public int getEffectId()
        {
            return _effectId;
        }

        override public void cure()
        {
            if ( _timer != null )
            {
                _timer.Dispose(); // 麻痺毒タイマー解除
            }

            _target.setPoisonEffect( 0 );
            _target.setPoison( null );
        }
    }

}
